/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.reportes;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author Raul
 */
public class GestorReporte {

    public static final String LOGO = "LOGO";
    public static final String EMPRESA = "NOMBRE_EMPRESA";
    public static final String ESLOGAN = "ESLOGAN";    
    public static final String TITULO = "TITULO_REPORTE";
    public static final String SUBREPORTE = "SUBREPORTE";
    
    private HashMap parametros;
    private String tituloVentana;
    private JRDataSource datos;
    private InputStream inputStream;

    public GestorReporte() {
        parametros = new HashMap();
        datos = new JREmptyDataSource();
    }
    
    public GestorReporte(InputStream inputStream, String tituloVentana) {
        this();
        this.inputStream = inputStream;
        this.tituloVentana = tituloVentana;
    }

    public String getTituloVentana() {
        return tituloVentana;
    }

    public void setTituloVentana(String tituloVentana) {
        this.tituloVentana = tituloVentana;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    
    public void setParametro(String parametro, Object valor){
        parametros.put(parametro, valor);
    }

    public void setDatos(List datos) {
        this.setParametro("SUBREPORTE_DATA_SOURCE", new JRBeanCollectionDataSource(datos));
    }
    
    public void mostrarReporte(){
        try {
            JasperPrint print = JasperFillManager.fillReport(inputStream, parametros, datos);
            print.setName(tituloVentana);
            
            JasperViewer jviewer = new JasperViewer(print, false);
            jviewer.setTitle(tituloVentana);
            jviewer.setVisible(true);
        } catch (JRException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
